#region Microsoft Public License
/*
 * (c) Copyright Chenjun Wu. http://utablesdk.codeplex.com
 * 
 * This source is subject to the Microsoft Public License (Ms-PL).
 * Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
 * All other rights reserved.
 */
#endregion

﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using UTable.Input;
using UTable.Input.MultiTouch;
using UTable.Objects;
using UTable.Objects.Controls;
using System.Windows.Media;
using System.ComponentModel;
using System.IO;
using System.Windows.Media.Imaging;

namespace UTable.Objects.Controls
{
	/// <summary>
	/// Interaction logic for UBookPreviewControl.xaml
	/// </summary>
	public partial class UBookPreviewControl : UUserControl, IControlDisposable
	{
		public UBookPreviewControl()
		{
			InitializeComponent();
			this.DataContext = this;
			DependencyPropertyDescriptor.FromProperty(UBookPreviewControl.PagesFolderPathProperty,
					typeof(UBookPreviewControl)).AddValueChanged(this, new EventHandler(OnPagesFolderPathChanged));
			this.Loaded += new RoutedEventHandler(UBookPreviewControl_Loaded);
		}

		private void UBookPreviewControl_Loaded(object sender, RoutedEventArgs e)
		{
			LoadCover();
		}

		private void OnPagesFolderPathChanged(object sender, EventArgs args)
		{
			LoadCover();
		}

		private void LoadCover()
		{
			if (PagesFolderPath != string.Empty)
			{
				String[] files = Directory.GetFiles(PagesFolderPath);
				List<String> fileList = new List<string>(files);
				fileList.Sort();
				foreach (String file in fileList)
				{
					if (File.Exists(file) && (file.EndsWith("album.png") || file.EndsWith("album.jpg") || file.EndsWith("book.png") || file.EndsWith("book.jpg")))
					{
						Cover = new BitmapImage(new Uri(Path.GetFullPath(file), UriKind.Absolute));
						break;
					}
				}
			}
		}

		public static readonly DependencyProperty PagesFolderPathProperty =
			DependencyProperty.Register("PagesFolderPath", typeof(String), typeof(UBookPreviewControl), new UIPropertyMetadata(string.Empty));

		public String PagesFolderPath
		{
			get { return (String)GetValue(PagesFolderPathProperty); }
			set { SetValue(PagesFolderPathProperty, value); }
		}

		public ImageSource Cover
		{
			get { return (ImageSource)GetValue(CoverProperty); }
			set { SetValue(CoverProperty, value); }
		}

		public static readonly DependencyProperty CoverProperty =
			DependencyProperty.Register("Cover", typeof(ImageSource), typeof(UBookPreviewControl), new UIPropertyMetadata(null));


        #region IControlDisposable Members

        public void DisposeControl()
        {
            DependencyPropertyDescriptor.FromProperty(UBookPreviewControl.PagesFolderPathProperty,
                typeof(UBookPreviewControl)).RemoveValueChanged(this, new EventHandler(OnPagesFolderPathChanged));
        }

        #endregion
    }
}
